/**
 * 班级信息管理
 */

$(function(){
	console.log("xxxxxxxxxxx");
	let reqParam = {};
	queryData(reqParam);
	teacher();
});
//条件查询班级信息(条件查询)

function queryClassRoom(){
	//组装参数
	let reqParam = {};
	reqParam.gradeName= $("#gradeName").val();
	reqParam.classTeacherName = $("#classTeacherName").val();
	reqParam.lecturerName = $("#lecturerName").val();
	// {"classroomNumber": "123"}
	//调用查询
	queryData(reqParam);
	
}
/**
 * 查询班级信息
 */
function queryData(reqParam){
	// 当前页
	reqParam.pageNum;
	if (reqParam.pageNum > 1) {
	} else {
		reqParam.pageNum = 1;
	}
	reqParam.pageSize = $("#pageSize").val();
	if(reqParam.pageSize == ""){
		$("#pageSize").val(5);
		reqParam.pageSize = $("#pageSize").val();
	}
	var url = $("#addr").val() +"/grade/queryData";
	
	$.ajax({
        type: "POST",//请求的类型
        url: url, // 请求的URL
        data: JSON.stringify(reqParam),  // 请求的数据 //reqParam对象转换为JSON格式的字符串
        dataType: "json",   // 返回的数据类型，json, html, xml, jquery.parseJson() 
        contentType: "application/json;charset=utf-8",  // 请求的数据格式，字符集
        async: true, // 可以不写， 是否异步请求
        success: function (data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作
			
			console.log("返回数据::: " + data);
			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
			//把页面写在html页面上
			let arrData =  data.data;
			$('#tab').bootstrapTable('destroy');
			$('#tab').bootstrapTable({
				// url: retData,
				data: arrData,
				columns: [{
					field: 'gradeName',
					title: '班级名称'
				}, {
					field: 'gradeDescribe',
					title: '班级描述'
				}, {
					field: 'studentsNumber',
					title: '班级人数'
				},{
					field: 'classTeacherName',
					title: '班主任'
				},{
					field: 'lecturerName',
					title: '讲师'
				},{
					field: 'classroomClassroomNumber',
					title: '所在教室	'
				},  {
					field: 'createTime',
					title: '创建时间',
					sortable: true,
					formatter: function (value, row , index){
						
						return getYMDHMS(value)
					}
				}, {
					field: 'updateTime',
					title: '修改时间',
					sortable: true,
					formatter: function (value, row , index){
						
						return getYMDHMS(value)
					}
				}, {
					field: 'pkId',
					title: '操作',
					formatter: function(value, row, index){//row是data返回的对象，index是返回的长度
						return  "<a href=" + $("#addr").val() + "/grade/xq?gradeId=" +row.gradeId + "><button class='btn btn-primary'>详情</button></a>"
						+"<button type='button' onclick='findId(\"" + row.gradeId + "\")' class='btn btn-primary' data-toggle='modal' data-target='#updatemodal' data-whatever='@fat'>修改</button>"
						+"<button type='button' onclick='findId(\"" + row.gradeId + "\")' class='btn btn-primary' data-toggle='modal' data-target='#deletemodal' data-whatever='@fat'>删除</button>" 
					}
				},] 
			});
			// 分页信息
			// data.pageinfo   
			let total = data.pageInfo.total;
			
			let pageSize = $("#pageSize").val();
			let buttonNumber = parseInt(total / pageSize);
			console.log("当前页：：："+buttonNumber)
			if(total % pageSize > 0){
				buttonNumber = buttonNumber + 1;
				// buttonNumber++;
			}
			
			let pagebutton;
			
			if (reqParam.pageNum == 1) {
				pagebutton ='<li class="page-item disabled"><a class="page-link" onclick="queryDataPage(\'previous\')" href="#">Previous</a></li>';
			} else {
				pagebutton ='<li class="page-item"><a class="page-link" onclick="queryDataPage(\'previous\')" href="#">Previous</a></li>';
			}
			
			/**
			 * <li class="page-item"><a class="page-link" onclick="queryDataPage('previous')" href="#">Previous</a></li>
					<li class="page-item"><a class="page-link" onclick="queryDataPage('1')" href="#">1</a></li>
					<li class="page-item"><a class="page-link" onclick="queryDataPage('2')" href="#">2</a></li>
					<li class="page-item"><a class="page-link" onclick="queryDataPage('3')" href="#">3</a></li>
					<li class="page-item"><a class="page-link" onclick="queryDataPage('next')" href="#">Next</a></li>
			 */

			for(let i = 1; i <= buttonNumber; i++) {
				if (i == reqParam.pageNum) {
					pagebutton = pagebutton + '<li class="page-item active"><a class="page-link" onclick="queryDataPage(\'' + i + '\')" href="#">'+ i +'</a></li>';
				} else {
					pagebutton = pagebutton + '<li class="page-item"><a class="page-link" onclick="queryDataPage(\'' + i + '\')" href="#">'+ i +'</a></li>';
				}
			}
			
			if (reqParam.pageNum == buttonNumber) {
				pagebutton = pagebutton + '<li class="page-item disabled"><a class="page-link" onclick="queryDataPage(\'next\')" href="#">Next</a></li>';
			} else {
				pagebutton = pagebutton + '<li class="page-item"><a class="page-link" onclick="queryDataPage(\'next\')" href="#">Next</a></li>';
			}
			$("#pagination").html(pagebutton);
        }
        
    });
}
function tz(){
 		window.location.href = "/ssb/src/main/resources/templates/dormitory/xiangqingxs.html"; // 跳转到指定的页面
}

// 状态转换
// 使用状态 0-使用 1-未使用 2-维修中
function useStatus(status) {
	if (status == "0") {
		return "使用";
	} else if (status == "1") {
		return "未使用";
	} else if (status == "2") {
		return "维修中";
	} else {
		return "错误";
	}
}


//时间格式转换
function getYMDHMS(timestamp) {
	let time = new Date(timestamp)
	let year = time.getFullYear()
	let month = time.getMonth() + 1
	let date = time.getDate()
	let hours = time.getHours()
	let minute = time.getMinutes()
	let second = time.getSeconds()

	if (month < 10) { month = '0' + month }
	if (date < 10) { date = '0' + date }
	if (hours < 10) { hours = '0' + hours }
	if (minute < 10) { minute = '0' + minute }
	if (second < 10) { second = '0' + second }
	return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
}

//新增
function save1(){
	var url = $("#addr").val() +"/grade/saveData";
	var reqParam = {};
	reqParam.gradeName = $("#add-gradeName").val();
	reqParam.gradeDescribe = $("#add-gradeDescribe").val();
	reqParam.classTeacherId = $("#add-classTeacherId").val();
	reqParam.lecturerId = $("#add-lecturerId").val();
	reqParam.classroomClassroomId = $("#add-classroomClassroomId").val();
	reqParam.classTeacherName = $("#add-classTeacherName").val();
	reqParam.lecturerName = $("#add-lecturerName").val();
	reqParam.classroomClassroomNumber = $("#add-salary").val();
	$.ajax({
        type: "POST",  
        url: url, // 请求的URL
        data: JSON.stringify(reqParam),  // 请求的数据
        dataType: "json",   // 返回的数据类型，json, html, xml, jquery.parseJson() 
        contentType: "application/json;charset=utf-8",  // 请求的数据格式，字符集
        async: true, // 可以不写， 是否异步请求
        success: function (data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作
		
			console.log("新增::: ");
			console.log("返回数据::: " + data);
			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
			//把页面写在html页面上
			/*var gradeId = arrData.gradeId;//获取教室Id把值给classroomId*/
			$("#addmodal").modal('hide');//关闭模态框
			if(data.code == "0000"){
				alert("添加成功");
			let reqParam = {};
			queryData(reqParam);
			}
				  
        }
        
    });
}

//班主任 讲师 教室
function teacher(){
	
	var url = $("#addr").val() + "/grade/teacher";
	
	$.ajax({
		type: "POST",
		url: url, // 请求的URL
		data: JSON.stringify({}),  // 请求的数据
		dataType: "json",   // 返回的数据类型，json, html, xml, jquery.parseJson() 
		contentType: "application/json;charset=utf-8",  // 请求的数据格式，字符集
		async: true, // 可以不写， 是否异步请求
		success: function(data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作

			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
			console.log("返回数据::: " + data.data);
			// 把数据写在页面上
			
			let redata = data.data;
			
			var str = "<option>请选择班主任</option>";

			for(let i = 0; i < redata.length; i++){
				str = str + "<option>" + redata[i].employeeName + "</option>";
			}
				$("#add-classTeacherName").html(str);
				//修改
				$("#update-classTeacherName").html(str);
			
			var str = "<option>请选择讲师</option>";

			for(let i = 0; i < redata.length; i++){
				str = str + "<option>" + redata[i].employeeName + "</option>";
			}
				$("#add-lecturerName").html(str);
				//修改
				$("#update-lecturerName").html(str);

				
				
				
			var redata1 = data.classroom;
			
			var str = "<option>请选择所在教室</option>";

			for(let i = 0; i < redata1.length; i++){
				str = str + "<option>" + redata1[i].classroomName + "</option>";
			}
				$("#add-salary").html(str);
				//修改
				$("#update-salary").html(str);

		}
		
	});
}





//反显
function findId(gradeId){
	
	var url = $("#addr").val() +"/grade/findgradeId";
	var reqParam = {};
	reqParam.gradeId = gradeId;
	console.log("gradeId::: " + gradeId);
	$.ajax({
        type: "POST",  
        url: url, // 请求的URL
        data: JSON.stringify(reqParam),  // 请求的数据
        dataType: "json",   // 返回的数据类型，json, html, xml, jquery.parseJson() 
        contentType: "application/json;charset=utf-8",  // 请求的数据格式，字符集
        async: true, // 可以不写， 是否异步请求
        success: function (data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作
			
			console.log("返回数据::: " + data);
			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
				//删除反显获取Id
				let redata = data.data;
				$("#gradeId").val(redata.gradeId); 
				//修改反显获取Id
				$("#update-gradeName").val(redata.gradeName);
				$("#update-gradeDescribe").val(redata.gradeDescribe);
				$("#update-classTeacherName").val(redata.classTeacherName);
				$("#update-lecturerName").val(redata.lecturerName);
				$("#update-salary").val(redata.classroomClassroomNumber);
				$("#gradeId").val(redata.gradeId);
				let reqParam = {};
				queryData(reqParam);
        }
    });
}
//删除
function del(){
	
	var url = $("#addr").val() + "/grade/deleteData";
	var reqParam = {};
	reqParam.gradeId = $("#gradeId").val();
	
	$.ajax({
        type: "POST",  
        url: url, // 请求的URL
        data: JSON.stringify(reqParam),  // 请求的数据
        dataType: "json",   // 返回的数据类型，json, html, xml, jquery.parseJson() 
        contentType: "application/json;charset=utf-8",  // 请求的数据格式，字符集
        async: true, // 可以不写， 是否异步请求
        success: function (data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作
			console.log("返回数据::: " + data);
			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
			$("#deletemodal").modal('hide');//关闭模态框
			let reqParam = {};
			queryData(reqParam);
        }
    });
}
//修改
function update(){
	var url = $("#addr").val() + "/grade/updateData";
	var reqParam = {};
	reqParam.gradeName = $("#update-gradeName").val();
	reqParam.gradeDescribe = $("#update-gradeDescribe").val();
	reqParam.classTeacherName = $("#update-classTeacherName").val();
	reqParam.lecturerName = $("#update-lecturerName").val();
	reqParam.classroomClassroomNumber = $("#update-salary").val();
	reqParam.gradeId = $("#gradeId").val();
	console.log("gradeId===++++++++++++== " + gradeId);
	$.ajax({
        type: "POST",  
        url: url, // 请求的URL
        data: JSON.stringify(reqParam),  // 请求的数据
        dataType: "json",   // 返回的数据类型，json, html, xml, jquery.parseJson() 
        contentType: "application/json;charset=utf-8",  // 请求的数据格式，字符集
        async: true, // 可以不写， 是否异步请求
        success: function (data, textStatus, jqXHR) {//success是一个回调函数，用来处理当AJAX请求成功完成时的操作
			
			console.log("返回数据::: " + data);
			console.log("返回数据::: " + data.code);
			console.log("返回数据::: " + data.msg);
			$("#updatemodal").modal('hide');//关闭模态框
			let reqParam = {};
			queryData(reqParam);
        }
    });
}
// 分页
function queryDataPage(param) {
	console.log("queryDataPage::: " + param);

	let pagenum = $(".active").children("a").html();
	console.log("pagenum::: " + pagenum);

	let reqParam = {};
	if ("previous" == param) {
		pagenum = pagenum - 1;
		reqParam.pageNum = pagenum;
		console.log("previous-reqParam.pageNum::: " + reqParam.pageNum)
	} else if ("next" == param) {
		//pagenum = pagenum + 1;
		console.info("param :" +pagenum);
		pagenum++;
		reqParam.pageNum = pagenum;
		console.log("next-reqParam.pageNum::: " + reqParam.pageNum)
	} else {
		reqParam.pageNum = param;
	}

	queryData(reqParam);
}

